import { fileURLToPath, URL } from "node:url";

import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';
import postcsspxtoviewport8plugin from "postcss-px-to-viewport-8-plugin";

const port = process.env.port || process.env.npm_config_port || 5170; // 端口

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const ENV = loadEnv(mode, ".", "VITE");
  return {
    base: './',
    plugins: [
      vue(),
      Components({
        resolvers: [VantResolver()],
      })
    ],
    server: {
      cors: true,
      port: port,
      proxy: {
        [ENV.VITE_APP_BASE_API]: {
          target: `https://ycfn.ydlweb.com`,
          changeOrigin: true,
          rewrite: (path) => {
            return path.replace(new RegExp(`${ENV.VITE_APP_BASE_API}`), "");
          },
        },
         [ENV.VITE_APP_PYTHON_API]: {
          target: `https://fileformat.000081.xyz`,
          changeOrigin: true,
          rewrite: (path) => {
            return path.replace(new RegExp(`${ENV.VITE_APP_PYTHON_API}`), "");
          },
        },
      },
    },
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    build: {
      outDir: 'cover-preview'
    },
    css: {
      postcss: {
        plugins: [
          postcsspxtoviewport8plugin({
            // UI稿是 750 兼容 vant
            viewportWidth: (file) => file.indexOf('vant') !== -1 ? 375 : 750,
            propList: ['*'],
            unitPrecision: 6, // 转换后的精度，即小数点位数 
            minPixelValue: 1, // 默认值1，小于或等于1px则不进行转换 
            replace: true, // 是否转换后直接更换属性值      
          })
        ]
      }
    }
  };
});
